package com.financing.platform.service.impl;

import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.dao.IJpushMessageDao;
import com.financing.platform.dao.INewsDao;
import com.financing.platform.dto.MessageJpushDTO;
import com.financing.platform.dto.NewsDTO;
import com.financing.platform.service.INewsService;

import cn.jpush.api.utils.Base64;

/**
 * 
* @ClassName: OrderServiceImpl 
* @Description: 机构管理
* @author chenming 
* @date 2014年8月18日 下午2:42:30 
*
 */
@Service("newsServiceImpl")
@Transactional(rollbackFor=Exception.class)
public class NewsServiceImpl extends BaseServiceImpl implements INewsService {

	@Autowired
	private INewsDao newsDaoImpl;
	@Autowired
	private IJpushMessageDao jPushMessageDaoImpl;
	
	@Value("#{settings['jiGuangPushRedrictNewsList']}")
	private String jpushRedrict;
	
	public int addNews(NewsDTO newsDTO) throws SQLException {
		int newsId = newsDaoImpl.addNews(newsDTO);
		/*// 极光推送消息至所有人
		MessageJpushDTO messageJpushDTO = new MessageJpushDTO();
		messageJpushDTO.setFpJpushTitle(newsDTO.getTitle());
		messageJpushDTO.setFpJpushContent(newsDTO.getContents());
		
//		messageJpushDTO.setFpJpushRedrict("/#/inform/zxgg/" + newsId + "");
		messageJpushDTO.setFpJpushRedrict(new String(Base64.encode(URLEncoder.encode(jpushRedrict).getBytes())));
		
		messageJpushDTO.setFpJpushMsgType("01");// 消息类型 01:系统消息 02:运营推送
		messageJpushDTO.setFpJpushSendType("01");// 推送消息类型  01:实时推送  02:定时推送
		messageJpushDTO.setCreateBy(newsDTO.getCreateBy());
		messageJpushDTO.setCreateDate(new Date());
		messageJpushDTO.setUpdateBy(newsDTO.getCreateBy());
		messageJpushDTO.setUpdateDate(new Date());
		jPushMessageDaoImpl.addJpushMessage(messageJpushDTO);*/
		return newsId;
	}

	public List<NewsDTO> queryNewsList(NewsDTO newsDTO) {
		return newsDaoImpl.queryNewsList(newsDTO);
	}

	public int deleteNews(String[] ids) throws SQLException {
		NewsDTO news = new NewsDTO();
		for(String id : ids){
			news.setId(Integer.valueOf(id));
			newsDaoImpl.deleteNews(news);
		}
		return ids.length;
	}

	public int updateNews(NewsDTO newsDTO) throws SQLException {
		return newsDaoImpl.updateNews(newsDTO);
	}

	@Override
	public int queryNewsListCount(NewsDTO newsDTO) throws SQLException {
		return newsDaoImpl.queryNewsListCount(newsDTO);
	}

}
